<?php
class Index extends Admin_Controller {

    public function __construct() {
        parent::__construct();
        $this->_load();
    }

    public function index() {
        $this->_replacePhrase();
    }

    public function save() {
        if (!(bool) $this->input->is_ajax_request()) {
            redirect('category/admincp/index');
            return false;
        }
        $aCateogry = $this->input->post('category');       
        $this->load->library('form_validation', null, 'validation');
        $this->validation->set_rules($this->category->getRule(empty($aCateogry['id'])? false: $aCateogry['id']));
        $this->validation->set_error_delimiters('<div class="error">', '</div>');

        if ($this->validation->run() === false) {
            $this->return_fail_json($this->category->getValidationError());
            return false;
        }

        if ((bool)$aCateogry) {
            //Save and edit in this.
            $varMessage = "phrase var='category.add_new_category_successfully'";
            if($aCateogry['id']){
                $varMessage = "phrase var='category.update_category_successfully'";
            }
                
            if ((bool) $aReturn = $this->category->save($aCateogry)) {
                $this->return_success_json($aReturn, $this->lang->line($varMessage));
                return true;
            }
        }
    }
    
    public function gets() {
        if (!(bool) $this->input->is_ajax_request()) {
            $this->_redirect_index();
        }

        $aCategory = $this->category->gets();
        $this->return_success_json($aCategory, '');
    }

    public function delete() {
        if (!(bool) $this->input->is_ajax_request()) {
            $this->_redirect_index();
        }

        $iId = $this->input->post('category_id');
        
        if ((bool) $result = $this->category->delete($iId)) {
            $this->return_success_json(array('id' => $iId), $this->lang->line("phrase var='category.delete_category_successfully'"));
            return true;
        }

        $this->return_fail_json(NULL, '');
    }

    public function get() {
        if (!(bool) $this->input->is_ajax_request()) {
            $this->_redirect_index();
        }

        $iId = $this->input->post('category_id');
        
        if ((bool) $oCategory = $this->category->getById($iId, false)) {
            $oCategory->children = $this->category->hasChildren($iId);
            $this->return_success_json($oCategory, '');
            return true;
        }

        $this->return_fail_json(NULL, '');
    }
    
    public function hasProduct(){
        if (!(bool) $this->input->is_ajax_request()) {
            $this->_redirect_index();
        }
        
        $id = $this->input->post('category_id');
        $bIsHasProduct = false;
        $result = $this->product->getByCategoryId($id);
        
        if($result){
            $bIsHasProduct = true;
        }
        
        $aReturn = array('has_product'=> $bIsHasProduct);
        $this->return_success_json($aReturn);
        return true;
        
        $this->return_fail_json(array());
    }
    
    private function _load() {
        $this->load->library('pagination');
        $this->load->helper(array('url', 'form'));
        $this->load->model(array('user', 'category'));
        $this->load->model('product', '', FALSE, 'product');
        $this->lang->load('admin/general', $this->_slang_name);
        $this->lang->load('admin/category', $this->_slang_name);
    }

    private function _replacePhrase() {
        $this->_adata['token'] = (object) array("name" => $this->security->get_csrf_token_name(), "value" => $this->security->get_csrf_hash());
        $this->_adata['phrase_js'] = json_encode($this->lang->language);
        $this->fill_phrase('index', $this->_adata, $this->lang->language);
    }
}
?>